Gitlab, Gitlab Pages, Gitlab Runnerをリバースプロキシの背後で動かす
目次
Gitlab, Gitlab Pages, Gitlab Runnerをリバースプロキシの背後で動かす
#概要
GitlabではSSLの設定を一切やらずにリバースプロキシに任せてGitlabとPages,Runnerを動かす。 GitlabでSSLの設定をすると証明書が二重に必要になったりして、めんどい。
#Gitlab, Gitlab Pages
Gitlab runnerだけ別のdocker-composeファイルで管理する。
まず、GitlabとGitlab Pages用のdocker-compose.ymlがこちら。*.example.comを適宜書き換えること。
version: '3.6'
services:
web:
image: 'gitlab/gitlab-ce:latest'
restart: unless-stopped
hostname: 'git.example.com'
environment:
GITLAB_OMNIBUS_CONFIG: |
# general
external_url 'https://git.example.com'
nginx['listen_port'] = 80
nginx['listen_https'] = false
# registry
registry_external_url 'https://cr.example.com'
registry_nginx['enable'] = true
registry_nginx['listen_port'] = 5050
registry_nginx['listen_https'] = false
# pages
pages_external_url 'https://pages.example.com'
registry_nginx['enable'] = true
pages_nginx['listen_port'] = 5056
pages_nginx['listen_https'] = false
gitlab_pages['internal_gitlab_server'] = 'http://localhost:80'
ports:
- '4444:80'
# - '22:22'
- '5055:5050'
- '5056:5056'
volumes:
- './data/config:/etc/gitlab'
- './data/logs:/var/log/gitlab'
- './data/data:/var/opt/gitlab'
shm_size: '256m'これでdocker-compose up -dをする。結構遅い。自分の非力なサーバーマシンでは3,4分かかった。
そして適宜ポートに適したリバールプロキシを設定する。
#ポイント解説
external_url: Gitlab本体のURL。{pages_, registry_}nginx['listen_https']: これがfalseになっていないとSSL証明書を設定しないといけなくなる。registry_external_url: Gitlab RegistryのURLpages_external_url: Gitlab pagesのURL。ユーザー/グループ名.pages_external_url/プロジェクト名のようなURLになる。
#Gitlab Pagesの別ドメイン
Gitlab PagesのURLはuser.page.example.com/repoのようになるわけだが、例えばrepo.example.comのようなドメインを割り振りたい時がある。
そのような時はGITLAB_OMNIBUS_CONFIGに
gitlab_pages['external_http'] = ['0.0.0.0:5057']このような感じのものを追加する。このIPとポートはバインドできるためならなんでもよい(カスタムドメインを有効化するためだけの設定で実際にこの値は使わない)。
次に、今回はプライベートなサーバーで面倒くさいのでドメインの検証を管理エリア -> 設定 -> 基本設定 -> Pages -> ユーザーにカスタムドメインの所有を証明することを要求する をオフにする。
この設定を終えるとリポジトリのページでドメインが追加できるようになっているため追加する。 するとDNSの設定でALIASレコードを追加しろと出るが、この方法だとGitlabに証明書を追加したりしないといけないため、今回はDNSではなくリバースプロキシを使う。
今回の自分の設定ではhttp://127.0.0.1:5056をrepo.example.comに転送する設定を作れば無事そのドメインでアクセスできるようになっているはずだ。
#CI Runner
version: '3.6'
services:
runner:
image: 'gitlab/gitlab-runner:latest'
restart: unless-stopped
volumes:
- ./config:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
ports:
- "8093:8093"これでdocker-compose up -dするとrunnerが起動する。
次に、これをGitlabに登録する。まず https://git.example.com/admin/runners/new を開いてtagsを適当に設定して、ランナーを作成する。するとトークンが表示されるのでこれをコピーしておく。次にrunnerのコンテナ内で
$ gitlab-runner register
を実行し、gitlabのURLと先ほどのトークンを入力する。エラーが出なければこれで成功。
#参考にしたもの
- https://gitlab-docs.creationline.com/runner/register/index.html#docker
- https://gitlab-docs.creationline.com/runner/install/docker.html
- https://docs.gitlab.com/runner/configuration/advanced-configuration.html
- https://gitlab-docs.creationline.com/runner/configuration/advanced-configuration.html